package huixue.cn.com.rxapp.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
import android.widget.Scroller;

import java.util.ArrayList;
import java.util.List;

import huixue.cn.com.library.util.PixelUtils;
import huixue.cn.com.rxapp.util.LogUtils;

/**
 * Created by gongdongyang on 2018/8/1.
 */

public class CustomView extends View {
    private List<Integer> list = new ArrayList<Integer>();

    private Paint mPaint;

    private Scroller mScroller;

    private VelocityTracker mVelocityTracker;

    private float mLastX = 0;

    /**
     * fling的最小阈值
     */
    private int mMinVelocity;

    /**
     * fling的最大速度
     */
    private int mMaxVelocity;

    /**
     * 最小的边界值
     */
    private int mMinScrollX;

    /**
     * 最大的边界值
     */
    private int mMaxScrollX;

    public CustomView(Context context) {
        super(context);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        java.util.Random r=new java.util.Random();
        for(int i=0;i<100;i++){
            list.add(r.nextInt(100));
        }

        mMinScrollX = 60;

        mMaxScrollX = 100*PixelUtils.dp2px(40)+60-PixelUtils.dp2px(300);

        mPaint = new Paint();
        mPaint.setColor(Color.GRAY);// 设置灰色
        mPaint.setStyle(Paint.Style.FILL);//设置填满

        mScroller = new Scroller(context);
        mVelocityTracker = VelocityTracker.obtain();

        ViewConfiguration viewConfiguration = ViewConfiguration.get(getContext());
        mMinVelocity = viewConfiguration.getScaledMinimumFlingVelocity();
        mMaxVelocity = viewConfiguration.getScaledMaximumFlingVelocity();

    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //绘制矩形
        drawRect(canvas);
        drawText(canvas);
    }

    private  void drawRect(Canvas canvas){
        mPaint.setColor(Color.GRAY);// 设置灰色
        mPaint.setStyle(Paint.Style.FILL);//设置填满
        for(int i=0;i<list.size();i++){
            canvas.drawRect(60+(i*PixelUtils.dp2px(10+30)), PixelUtils.dp2px(300)- PixelUtils.dp2px(50+list.get(i)), 60+(i*PixelUtils.dp2px(10+30))+PixelUtils.dp2px(30), PixelUtils.dp2px(300), mPaint);// 正方形
        }
    }

    private void drawText(Canvas canvas){
        mPaint.setColor(Color.BLACK);
        mPaint.setTextSize(PixelUtils.dp2px(10));
        mPaint.setStyle(Paint.Style.STROKE);//设置填满
        for(int i=0;i<list.size();i++){
            canvas.drawText((i+1)+"",60+(i*PixelUtils.dp2px(10+30)),(PixelUtils.dp2px(300)- PixelUtils.dp2px(50+list.get(i)))/2,mPaint);
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();//获得VelocityTracker类实例
        }
        mVelocityTracker.addMovement(event);

        float currentX = event.getX();
        LogUtils.e("currentX=="+currentX);
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
            {
                getParent().requestDisallowInterceptTouchEvent(true);
                if (mScroller != null && !mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }
                mVelocityTracker.clear();
                mLastX = currentX;
                return true;
            }

            case MotionEvent.ACTION_MOVE:
            {
                int dx = (int) (mLastX - currentX);
                scrollBy(dx, 0);
                mLastX = currentX;
                return true;
            }

            case MotionEvent.ACTION_UP:
            {
                final VelocityTracker velocityTracker = mVelocityTracker;
                // 1000 provides pixels per second
                // 设置maxVelocity值为0.01时，速率大于0.01时，显示的速率都是0.01,速率小于0.01时，显示正常
                velocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
                float xVelocity = velocityTracker.getXVelocity();
                if (Math.abs(xVelocity) >= mMinVelocity) {
                    fling(-(int) xVelocity);
                } else {
                    scrollExactly();
                }

                velocityTracker.clear();
                return true;
            }
        }
        return super.onTouchEvent(event);
    }

    public void fling(int velocityX) {
        mScroller.fling(getScrollX(), getScrollY(), velocityX, 0, 0, mMaxScrollX, 0,0);
    }

    private void scrollExactly() {
       /* mCurrentScale = Math.round(mCurrentScale);

        mScroller.startScroll(getScrollX(), getScrollY(),
                scale2ScrollX(mCurrentScale) - getScrollX(), 0, 1000);*/

        invalidate();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        LogUtils.e("computeScroll==");
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            invalidate();
        }
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
    }
}
